//SmartEarth 改 新增文件
import clone from "../Core/clone.js";
import Texture from "../Renderer/Texture"
import PixelFormat from "../Core/PixelFormat"
import PixelDatatype from "../Renderer/PixelDatatype"
import Sampler from "../Renderer/Sampler"
import TextureWrap from "../Renderer/TextureWrap"
import TextureMinificationFilter from "../Renderer/TextureMinificationFilter"
import TextureMagnificationFilter from "../Renderer/TextureMagnificationFilter"

function getTexture(e, frameState) {
  return new Texture({
    context: frameState.context,
    pixelFormat: PixelFormat.RGBA,
    pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
    source: {
      width: 1,
      height: 1024,
      arrayBufferView: e
    },
    sampler: new Sampler({
      wrapS: TextureWrap.CLAMP_TO_EDGE,
      wrapT: TextureWrap.CLAMP_TO_EDGE,
      minificationFilter: TextureMinificationFilter.LINEAR,
      magnificationFilter: TextureMagnificationFilter.LINEAR
    })
  })
}

var ExpandBySmartEarth = {
  trustGenerator: ["fanfan"],
  _defaultFloodAnalysis: {
    floodVar: [0, 0, 0, 500],
    ym_pos_x: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ym_pos_y: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ym_pos_z: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    rect_flood: [0, 0, 0, 0, 0, 0, 0, 0, 0],
    floodSpeed: 1,
    ym_max_index: 0,
    globe: !0,
    showElseArea: !0,
  },
  floodAnalysis: {
    floodVar: [0, 0, 0, 500],
    ym_pos_x: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ym_pos_y: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    ym_pos_z: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    rect_flood: [0, 0, 0, 0, 0, 0, 0, 0, 0],
    floodSpeed: 1,
    ym_max_index: 0,
    globe: !0,
    showElseArea: !0,
  },
  resetFloodAnalysis: function() {
    this.floodAnalysis = clone(this._defaultFloodAnalysis);
  },
  _defaultExcavateAnalysis: {
    splitNum: 30,
    showSelfOnly: !1,
    dig_pos_x: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    dig_pos_y: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    dig_pos_z: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    rect_dig: [0, 0, 0, 0, 0, 0, 0, 0, 0],
    dig_max_index: 0,
    excavateHeight: 0,
    excavateMinHeight: 9999,
    excavatePerPoint: !1,
  },
  excavateAnalysis: {
    splitNum: 30,
    showSelfOnly: !1,
    dig_pos_x: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    dig_pos_y: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    dig_pos_z: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    rect_dig: [0, 0, 0, 0, 0, 0, 0, 0, 0],
    dig_max_index: 0,
    excavateHeight: 0,
    excavateMinHeight: 9999,
    excavatePerPoint: !1,
  },
  resetExcavateAnalysis: function() {
    this.excavateAnalysis = clone(this._defaultExcavateAnalysis);
  },
  _defaultTilesEditor: {
    floodVar: [0, 0, 0, 0],
    flatRect: [0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_mat_x: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_mat_y: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_mat_z: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_max_index: 0,
    model_min_height: 50,
    IsYaPing: [!1, !1, !1, !1],
    yp_show_InOrOut: [!1, !0, !0, !1],
    yp_black_texture: null,
    hm_dh_attr: [50, 1, 100],
    modelLight: 2.2,
    times: new Date().getTime(),
    floodColor: [0, 0, 0, 0.5],
  },
  tilesEditor: {
    floodVar: [0, 0, 0, 0],
    flatRect: [0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_mat_x: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_mat_y: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_mat_z: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
    yp_max_index: 0,
    model_min_height: 50,
    IsYaPing: [!1, !1, !1, !1],
    yp_show_InOrOut: [!1, !0, !0, !1],
    yp_black_texture: null,
    hm_dh_attr: [50, 1, 100],
    modelLight: 2.2,
    times: new Date().getTime(),
    floodColor: [0, 0, 0, 0.5],
  },
  resetTilesEditor: function() {
    this.tilesEditor = clone(this._defaultTilesEditor);
  },
  underEarth: {
    cullFace: void 0,
    enable: void 0,
    enableDepth: 100,
    enableSkirt: !1,
  },
  occlusionOpen: !0,
  //坐标系默认4490
  Ellipsoid: {
    // x: 6378137.0, y: 6378137.0, z: 6356752.31414
    x: 6378137.0,
    y: 6378137.0,
    z: 6356752.31414035585,
  },
  //坡度分析
  displaySlopeMap: false,
  smoothSlopeAtHigherAlt: true,
  isSlopeDirty: !0,
  _sTex: [],
  get SlopeMapPallete() {
    return this._sTex.length <= 0 && (this._sTex.push({
      val: 100 * Math.cos(Cesium.Math.toRadians(90)),
      color: "#FF0000"
    }),
      this._sTex.push({
        val: 100 * Math.cos(Cesium.Math.toRadians(70)),
        color: "#FF0000"
      }),
      this._sTex.push({
        val: 100 * Math.cos(Cesium.Math.toRadians(45)),
        color: "#FF8000"
      }),
      this._sTex.push({
        val: 100 * Math.cos(Cesium.Math.toRadians(31)),
        color: "#FFF000"
      }),
      this._sTex.push({
        val: 100 * Math.cos(Cesium.Math.toRadians(17)),
        color: "#00FF00"
      }),
      this._sTex.push({
        val: 100 * Math.cos(Cesium.Math.toRadians(6)),
        color: "#0000FF"
      }),
      this._sTex.push({
        val: 100 * Math.cos(Cesium.Math.toRadians(0)),
        color: "#00FFFF"
      })),
      this._sTex
  },
  set SlopeMapPallete(e) {
    this._sTex = e
  },
  createElevationTextureFromPallete: function(e, frameState) {
    var n = 1024
      , r = new Uint8Array(n * 4)
      , i = r.length
      , s = e[0].val
      , o = e[e.length - 1].val
      , u = s - o
      , a = o
      , f = n / u
      , l = 0;
    l = e.length;
    for (var c = l - 1; c > 0; c--) {
      var h = e[c].color.replace("#", "")
        , p = e[c - 1].color.replace("#", "")
        , d = parseInt(h, 16)
        , v = parseInt(p, 16)
        , m = e[c].val
        , g = e[c - 1].val
        , y = Math.floor((m - a) * f + .5)
        , b = Math.floor((g - a) * f + .5)
        , w = b - y;
      if (w == 0)
        continue;
      var E = (d & 16711680) >> 16 << 16, S = (d & 65280) >> 8 << 16, x = (d & 255) << 16, T = (v & 16711680) >> 16 << 16, N = (v & 65280) >> 8 << 16, C = (v & 255) << 16, k = Math.floor((T - E) / w), A = Math.floor((N - S) / w), O = Math.floor((C - x) / w), M, _, D;
      M = 0,
        D = 0;
      for (var P = y; P < b; P++) {
        var H;
        E += k,
          S += A,
          x += O,
          M += D,
          r[P * 4 + 0] = E >> 16,
          r[P * 4 + 1] = S >> 16,
          r[P * 4 + 2] = x >> 16,
          r[P * 4 + 3] = 255
      }
    }
    var _texture = getTexture(r, frameState);
    return this._slopeTex = _texture,
      this.slopeMapTexture = this._slopeTex,
      _texture
  },
  setSlopeMapPallete: function(data) {
    var arr = [];
    data = Object.entries(data);
    data.length > 0 && data.forEach(function(item) {
      item.length == 2 && item[0] >= 0 && item[0] <= 90 && arr.push({
        val: 100 * Math.cos(Cesium.Math.toRadians(item[0])),
        color: item[1]
      })
    });
    if (arr.length === 0) {
      console.log("Error setting new pallete for slope map");
      return;
    }
    arr.sort(function(e, t) {
      return e.val < t.val ? -1 : t.val < e.val ? 1 : 0
    });
    this.SlopeMapPallete = arr;
    this.isSlopeDirty = !0;
  }
}

export default ExpandBySmartEarth;
//SmartEarth 改
